// pages/energy/index.js
const App=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bj: App.Imges.bj2,
    one: App.Imges.one,
    two: App.Imges.two,
    three: App.Imges.three,
    shu: App.Imges.shu,
    qiu: App.Imges.qiu,
    blankImg: App.Imges.blankImg,
    typeStata:true,
    qiuList:[],
    list:[],//排行榜
      
  },
  getrand(m, n){
    let rand = Math.floor(Math.random() * (n - m + 1)) + m;
    return rand
  },
  
  unreceive(){
    let qiuList = this.data.qiuList;
    App.ajax('mp/energy/unreceive/list', 'GET').then(res => {
      if (res.data.length == 0){
        return
      }
      qiuList=[...res.data];
      let query = wx.createSelectorQuery();
      let minW = 50, maxW = wx.getSystemInfoSync().windowWidth - 126, minh = 48, maxh = 0, x, y;
      query.select('.navShu').boundingClientRect((rect) => {
        maxh = rect.height
        qiuList.forEach((i, t) => {
          i.x = this.getrand(minW, maxW);
          i.y = this.getrand(minh, maxh / 2);
          i.stata = true;
        })
        this.setData({
          qiuList
        })
      }).exec();
    }).catch(err => {
      console.log(err)
    })
  },
  
  rank(){
    let list = this.data.list;
    App.ajax('mp/energy/rank/list', 'GET').then(res => {
      list=[...res.data];
      this.setData({
        list
      })

    }).catch(err => {
      console.log('报错了', err)
    })
  },
  blank(){
    wx.navigateBack({
      delta:1
    })
  },
  //收集
  collect(e){
    if (this.data.typeStata){
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let qiuList = this.data.qiuList;
      App.ajax(`mp/energy/receive/${id}`, 'PUT').then(res => {
        qiuList[index].stata = false;
        this.setData({
          typeStata: false,
          qiuList

        })
        setTimeout(() => {
          qiuList.splice(index, 1);
          this.setData({
            qiuList,
            typeStata: true
          })
        }, 1000)

      }).catch(err => {
        console.log('报错了', err)
      })
   
   
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.unreceive();
    this.rank();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})